﻿#include "MinerOwnedStates.h"
#include "State.h"
#include "Miner.h"
#include "Location.h"
#include "ConsoleUtils.h"
#include "EntityNames.h"

#include <iostream>
using std::cout;

#ifdef TEXTOUTPUT
#include <fstream>
extern std::ofstream os;
#define cout os
#endif

// EnterMinerAndDigForNugget
// SINGLETON design pattern

EnterMineAndDigForNugget *EnterMineAndDigForNugget::Instance()
{
	static EnterMineAndDigForNugget instance;
	return & instance;
}

void EnterMineAndDigForNugget ::Enter (Miner* pMiner)
{
	if(pMiner->Location() != goldmine)
	{
		SetTextColor (FOREGROUND_BLUE );
		cout<< "\n "<< GetNameOfEntity(pMiner->ID()) <<" Go to Goldmine";

		pMiner->ChangeLocation(goldmine);
	}
}

void EnterMineAndDigForNugget::Execute(Miner* pMiner)
{
	pMiner->AddToGoldCarried(1);
	pMiner->IncreaseFatidue();

	SetTextColor (FOREGROUND_BLUE);

	cout<<"\n " <<GetNameOfEntity(pMiner->ID()) <<": Dao Vang  ";

	if(pMiner->Fatigued())
	{
		pMiner->ChangeState(GoHomeAndSleepTilRested::Instance());
	}

	if(pMiner->IsPocketFull())
	{
		pMiner->ChangeState(VisitBankAndDepositGold::Instance());
	}
	if(pMiner->Thirsty())
	{
		pMiner->ChangeState(QuenchThirst::Instance());
	}
}

void EnterMineAndDigForNugget::Exit(Miner* pMiner)
{
	SetTextColor (FOREGROUND_BLUE);
	cout<<"\n "<<GetNameOfEntity(pMiner->ID())
				<<" : Yeah, ra khoi Goldmine voi tui day vang, haha...";
}


// VisitBankAndDepositGold
VisitBankAndDepositGold* VisitBankAndDepositGold::Instance()
{
	static VisitBankAndDepositGold instance;
	return &instance;
}

void VisitBankAndDepositGold ::Enter(Miner *pMiner)
{
	if(pMiner->Location() != bank)
	{
		SetTextColor (FOREGROUND_BLUE);
		cout<<"\n  " << GetNameOfEntity(pMiner->ID()) 
					<<" Dang di den Bank";
		pMiner->ChangeLocation(bank);
	}
}

void VisitBankAndDepositGold::Execute(Miner *pMiner)
{
	pMiner->AddToWealth(pMiner->GoldCarried());
	pMiner->SetGoldCarried(0);

	SetTextColor (FOREGROUND_BLUE);
	cout<<"\n  " << GetNameOfEntity (pMiner->ID()) 
				<<" Da gui Vang. Trong tai khoan hien co: "
				<<pMiner->Wealth();

	// nếu đã giàu thì
	if(pMiner->Thirsty())
	{
		pMiner->ChangeState(QuenchThirst::Instance()); return;
	}
	if(pMiner->Wealth() >=ComfortLevel)
	{
		SetTextColor (FOREGROUND_BLUE);
		cout<<"\n  "<< GetNameOfEntity(pMiner->ID() )
				  <<" Wohoo! Giau roi, ve nha nghi ngoi ^.^ ";
		pMiner->ChangeState(GoHomeAndSleepTilRested::Instance());
	}

	// nếu chưa giàu thì kiếm vàng tiếp
	else
	{
		pMiner->ChangeState(EnterMineAndDigForNugget::Instance());
	}

}

void VisitBankAndDepositGold::Exit(Miner* pMiner)
{
	SetTextColor (FOREGROUND_BLUE);
	cout<<"\n  "<<GetNameOfEntity (pMiner->ID())
			   <<" Ra khoi Ngan hang ";
}

GoHomeAndSleepTilRested * GoHomeAndSleepTilRested ::Instance()
{
	static GoHomeAndSleepTilRested instance;
	return& instance;
}

void GoHomeAndSleepTilRested::Enter(Miner* pMiner)
{
	if(pMiner->Location() != shack)
	{
		SetTextColor (FOREGROUND_BLUE);
		cout<<"\n   " <<GetNameOfEntity (pMiner->ID())
					<<" Dang di ve Nha";
		pMiner->ChangeLocation(shack);
	}
}

void GoHomeAndSleepTilRested::Execute(Miner *pMiner)
{
	if(pMiner->Thirsty())
	{
		pMiner->ChangeState(QuenchThirst::Instance()); return;
	}
	if(! pMiner->Fatigued())
	{
		SetTextColor (FOREGROUND_BLUE);
		cout<<"\n   " << GetNameOfEntity (pMiner->ID())
					<<" den luc di kiem them GOLD roi, hoho! ";
		if(!pMiner->Wealth())
			pMiner->ChangeState(EnterMineAndDigForNugget::Instance());
		else
			pMiner->ChangeState(QuenchThirst::Instance());
	}
	else
	{
		pMiner->DecreaseFatigue();
		SetTextColor (FOREGROUND_BLUE);
		cout<<"\n   "  <<GetNameOfEntity(pMiner->ID())
					<<":.zZz...zZZz.zZ.. Zz. ";
	}
}

void GoHomeAndSleepTilRested::Exit(Miner* pMiner)
{
	SetTextColor (FOREGROUND_BLUE);
	cout<<"\n   "	<< GetNameOfEntity(pMiner->ID())
				<< "Ra khoi Nha ";

}

//    QuenchThirst

QuenchThirst* QuenchThirst::Instance()
{
	static QuenchThirst instance;
	return& instance;
}

void QuenchThirst ::Enter(Miner *pMiner)
{
	if(pMiner->Location() != saloon)
	{
		pMiner->ChangeLocation(saloon);
		SetTextColor (FOREGROUND_INTENSITY);
		cout<<"\n"	<<GetNameOfEntity(pMiner->ID())
					<<" Khat Nuoc qua---> den Saloon thoi ~.^";
	}
}

void QuenchThirst ::Execute(Miner *pMiner)
{
	//if(pMiner->Thirsty())

	{
		pMiner->BuyAndDrink();
	 	SetTextColor (FOREGROUND_RED);
		cout<<"\n"	<< GetNameOfEntity (pMiner->ID() )
					<<" Lam Chai Bia & xa Stress nao, 123 ZO.. 2 3 ZO";
		if(pMiner->Wealth() >= ComfortLevel)
			pMiner->ChangeState (GoHomeAndSleepTilRested::Instance());
		else
			pMiner->ChangeState(EnterMineAndDigForNugget::Instance());
	}


	//else
	//{
	//	SetTextColor (FOREGROUND_BLUE);
	//	cout<<"\n	ERROR! ERROR! ERROR! ";
	//}
}

void QuenchThirst::Exit(Miner *pMiner)
{
	SetTextColor (FOREGROUND_GREEN);
	cout<<"\n"	<<GetNameOfEntity (pMiner->ID())
				<<" Roi khoi Sallon, Sang khoai :))";
}